package com.laker.autotest.webui.dev;

import cn.hutool.core.collection.CollUtil;
import com.laker.autotest.webui.utils.PlaywrightUtil;
import com.microsoft.playwright.*;
import lombok.val;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 绕过反爬虫策略 测试
 * <a href="https://github.com/playwright-community/playwright-go">...</a>
 * <a href="https://github.com/AtuboDad/playwright_stealth/blob/main/playwright_stealth/js/navigator.webdriver.js">参考</a>
 * <a href="https://www.browserscan.net/bot-detection">检测</a>
 * 1. navigator.webdriver 检测
 * 2. CDP检测 实际上这个网站只检测 console.debug，只需将这个函数换成一个空的就行了
 * </p>
 */
public class StealthTest {
    public static void main(String[] args) throws InterruptedException {
        PlaywrightUtil.installBrowser("chromium");
        try (Playwright playwright = PlaywrightUtil.initPlaywright()) {
            BrowserType chromium = playwright.chromium();
            // Make sure to run headed.
            Browser browser = chromium.launch(new BrowserType.LaunchOptions()
                    .setArgs(CollUtil.newArrayList("--start-maximized"
                                    // 绕过反爬虫策略 禁用 Blink 引擎中的 AutomationControlled 特性，从而减少被反爬机制识别为自动化工具的风险。
                                    , "--disable-blink-features=AutomationControlled"
                            )
                    )
                    // 绕过反爬虫策略  使用系统 Chrome 避免检测 就这个有用，使用真实的系统浏览器
                    // chrome msedge
                    // 注意所有chrome进程要关闭
//                    .setChannel("chrome")
                    .setHeadless(false));
            // Setup context however you like.
            val context = browser.newContext(new Browser.NewContextOptions()
                    //  绕过反爬虫策略 修改 User-Agent
//                    .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
                    .setViewportSize(null)
                    .setJavaScriptEnabled(true));
            // 绕过反爬虫策略
//            addStealthScripts(context);
            // Pause the page, and start recording manually.
            Page page = context.newPage();
            page.navigate("https://bot.sannysoft.com/");
            context.newPage().navigate("https://www.browserscan.net/zh/bot-detection");
            context.newPage().navigate("https://fingerprintjs.github.io/BotD/main/");
            //  cloudflare
            context.newPage().navigate("https://workupjob.com/");
            System.out.println(page.title());
            TimeUnit.MINUTES.sleep(15);
        }
    }

    /**
     * 添加绕过 WebDriver/CDP 检测的 JS 代码
     */
    private static void addStealthScripts(BrowserContext context) {
        //  在浏览器控制台测试 navigator.webdriver 测试,不加下面的则是true
        //  手动打开的浏览器，那么该属性为false
        context.addInitScript(
                // 绕过CDP检测
                "console.debug = () => {};"
                // get: () => undefined 会导致 navigator.webdriver 为 undefined
                // 目前这个没用垃圾，用 --disable-blink-features=AutomationControlled 代替
//                , "Object.defineProperty(navigator, 'webdriver', { get: () => false });"
//                        + "Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5] });"
//                        + "Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });"
        );
    }
}
